package com.jsjosem.aftersales.ui.worktickets;

import static com.jsjosem.aftersales.utils.CommonUtils.showBigImg;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.ImgListViewAdapter;
import com.jsjosem.aftersales.component.CustomGridView;
import com.jsjosem.aftersales.data.response.ImgResponse;
import com.jsjosem.aftersales.data.response.Product;
import com.jsjosem.aftersales.data.response.WorkTicket;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;

public class TicketInfoFragment extends Fragment {

    private Context context;
    private TextView ticketCodeTv, ticketTypeTv, deviceTv, serviceTypeTv, faultDescriptionTv, createByTv, createTimeTv;
    private CustomGridView imgGridView;
    private final WorkTicket workTicket;
    private View view;

    public static TicketInfoFragment newInstance(WorkTicket workTicket) {
        return new TicketInfoFragment(workTicket);
    }

    private TicketInfoFragment(WorkTicket workTicket) {
        this.workTicket = workTicket;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initView(inflater, container);
        initData();
        initImgList();
        return view;
    }

    private void initData() {
        ticketCodeTv.setText(workTicket.getTicketCode());
        ticketTypeTv.setText(workTicket.getTicketType());
        List<String> products = new ArrayList<>();
        for (Product product : workTicket.getOriginalTicket().getmProducts()) {
            if (product.getProduct() != null) {
                products.add(product.getProduct().getmName());
            }
        }
        deviceTv.setText(String.join(",", products));
        serviceTypeTv.setText(workTicket.getOriginalTicket().getmServiceType());
        faultDescriptionTv.setText(workTicket.getOriginalTicket().getmDescription());
        createByTv.setText(workTicket.getOriginalTicket().getmCreateUser().getRealname());
        createTimeTv.setText(workTicket.getOriginalTicket().getmCreateTimeFormat());
    }

    private void initImgList() {
        List<ImgResponse> images = workTicket.getOriginalTicket().getmImages();
        if (CollUtil.isNotEmpty(images)) {
            List<String> urls = images.stream().map(ImgResponse::getImg_url).collect(Collectors.toList());
            ImgListViewAdapter imgListViewAdapter2 = new ImgListViewAdapter(context, urls);
            imgGridView.setAdapter(imgListViewAdapter2);
            imgGridView.setOnItemClickListener((adapterView, view, i, l) -> showBigImg(context, urls, i));
        }
    }

    private void initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_ticket_info, container, false);
        context = view.getContext();
        ticketCodeTv = view.findViewById(R.id.ticketCodeTv);
        ticketTypeTv = view.findViewById(R.id.ticketTypeTv);
        deviceTv = view.findViewById(R.id.deviceTv);
        serviceTypeTv = view.findViewById(R.id.serviceTypeTv);
        faultDescriptionTv = view.findViewById(R.id.faultDescriptionTv);
        createByTv = view.findViewById(R.id.createByTv);
        createTimeTv = view.findViewById(R.id.createTimeTv);
        imgGridView = view.findViewById(R.id.imgGridView);
    }

}
